package cz.oplevka.lahvodroid;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;



public class Lahvodroid extends TabActivity /*implements TabHost.OnTabChangeListener*/{

    
    /** Called when the activity is first created. */
    private static final int MENU_ITEM_1 = Menu.FIRST + 1;
    private static final int MENU_ITEM_2 = Menu.FIRST + 2;
    private static final int MENU_ITEM_3 = Menu.FIRST + 3;

	
        TabHost tabHost;
        private static XMLBeerList xmlBeerList = null;
        Toast msg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        parseXML();
        tabHost = getTabHost(); 
        TabHost.TabSpec spec;  
        Intent intent;  
  
        intent = new Intent().setClass(this, FirstActivity.class);  
        spec = tabHost.newTabSpec("Scan").setIndicator("Scan")  
                      .setContent(intent);  
        tabHost.addTab(spec);
        
//        intent = new Intent().setClass(this, XMLParsing.class);  
//        spec = tabHost.newTabSpec("Vypis").setIndicator("Vypis")  
//                      .setContent(intent);  
//        tabHost.addTab(spec);
  
        intent = new Intent().setClass(this, MyListView.class);  
        spec = tabHost.newTabSpec("Seznam").setIndicator("Seznam")  
                      .setContent(intent);  
        tabHost.addTab(spec);
        
//        intent = new Intent().setClass(this, MapView.class);  
//        spec = tabHost.newTabSpec("HashMap").setIndicator("HashMap")  
//                      .setContent(intent);  
//        tabHost.addTab(spec);  

    }
    @Override
     public boolean onCreateOptionsMenu(Menu menu)
     {
        menu.add(Menu.NONE, MENU_ITEM_1, Menu.NONE,
            "Add").setIcon(R.drawable.ic_menu_add);

        menu.add(Menu.NONE, MENU_ITEM_2, Menu.NONE,
        "Export").setIcon(R.drawable.ic_menu_save);

        menu.add(Menu.NONE, MENU_ITEM_3, Menu.NONE,
        "Close").setIcon(R.drawable.ic_menu_close_clear_cancel);

        return (super.onCreateOptionsMenu(menu));

       }

        @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
        switch (item.getItemId()){
        case MENU_ITEM_1:
            Intent intent = new Intent(this, AddActivity.class);
            this.startActivity(intent);
        break;

        case MENU_ITEM_2:
        msg = Toast.makeText(Lahvodroid.this,
                "Menu Item 2 Clicked", Toast.LENGTH_LONG);
        msg.show();
        break;


        case MENU_ITEM_3:
            finish();                
        }
    return (super.onOptionsItemSelected(item));
    }


    public static void parseXML() {        
          try {			
			/** Handling XML */
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
                        
                        String SD_CARD_PATH = Environment.getExternalStorageDirectory().toString();
                        File file = new File(SD_CARD_PATH + "/Lahvodroid/test.xml");

//			/** Send URL to parse XML Tags */
//			URL sourceUrl = new URL(
//					"http://www.srandapage.webz.cz/test.xml");

			/** Create handler to handle XML Tags ( extends DefaultHandler ) */
			XMLHandler myXMLHandler = new XMLHandler();
			xr.setContentHandler(myXMLHandler);
//			xr.parse(new InputSource(sourceUrl.openStream()));
			xr.parse(new InputSource(new FileInputStream(file)));
                        
		} catch (Exception e) {
			System.out.println("XML Pasing Excpetion = " + e);
		}

		/** Get result from XMLHandler SitlesList Object */
		xmlBeerList = XMLHandler.xmlBeerList;
    } 
    
    /**
     * @return the xmlBeerList
     */
    public static XMLBeerList getXmlBeerList() {
        return xmlBeerList;
    }
}